package Sort.sort2021;

/**
 * @author leiwenkang <leiwenkang@kuaishou.com>
 * Created on 2021-03-12
 */
public class Sort冒泡排序 {
    public static void main(String[] args) {
        int[] array = {3, 5, 7, 2, 1, 4, 8, 9, 6};
        sort(array);
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
    }

    public static void sort(int[] array) {
        if (array == null || array.length == 0) {
            return;
        }

        boolean flag = true;
        for (int i = 0; i < array.length && flag; i++) {
            flag = false;
            for (int j = array.length - 1; j > i; j--) {
                if (array[j - 1] > array[j]) {
                    //交换
                    exchange(array, j - 1, j);
                    flag = true;
                }
            }
        }

    }

    private static void exchange(int[] array, int i, int j) {
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}
